package com.example.administrator.questionanswer;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.example.administrator.questionanswer.adapter.AnswerShowAdapter;
import com.example.administrator.questionanswer.application.MyApplication;
import com.example.administrator.questionanswer.dao.AnswerBeanDao;
import com.example.administrator.questionanswer.dao.DaoSession;
import com.example.administrator.questionanswer.greendao.AnswerBean;
import com.example.administrator.questionanswer.utils.Const;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/4/6 0006.
 */

public class DataShowActivity extends Activity {
    @BindView(R.id.txt_major)
    TextView txtMajor;
    @BindView(R.id.recycle)
    RecyclerView recycle;
    DaoSession daoSession;

    AnswerShowAdapter answerShowAdapter;
    @BindView(R.id.txt_user_name)
    TextView txtUserName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_show);
        ButterKnife.bind(this);
        SharedPreferences sharedPreferences = getSharedPreferences(Const.USER_INFO, MODE_PRIVATE);
        String userName = sharedPreferences.getString(Const.USER_NAME, null);
        String major = sharedPreferences.getString(Const.USER_MAJOR, null);
        daoSession = MyApplication.getInstances().getDaoSession();
        if (userName != null) {
            txtUserName.setText("姓名：" + userName);
            txtMajor.setText("专业：" + major);
            AnswerBeanDao answerBeanDao = daoSession.getAnswerBeanDao();
            List<AnswerBean> answerBeans = answerBeanDao.queryBuilder().where(AnswerBeanDao.Properties.UserName.eq(userName)).orderAsc(AnswerBeanDao.Properties.QuestionId).list();
            if (answerBeans.size() > 0) {
                answerShowAdapter = new AnswerShowAdapter(this);
                answerShowAdapter.setList(answerBeans);
                recycle.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
                recycle.setAdapter(answerShowAdapter);
                answerShowAdapter.notifyDataSetChanged();
            }
        }


    }
}
